<!DOCTYPE html>
<html>
<head>
  <script src="../../processing.js"></script>
  <script src="../init.js"></script>
<link rel="stylesheet" href="../style.css"/></head>
<body><h1><a href="http://ejohn.org/blog/processingjs/">Processing.js</a></h1>
<h2>LoadFile2</h2>

<p>This example loads a data file about cars. Each element is separated
with a tab and corresponds to a different aspect of each car. The file stores 
the miles per gallon, cylinders, displacement, etc., for more than 400 different
makes and models. Press a mouse button to advance to the next group of entries.</p>

<p><a href="http://processing.org/learning/topics/loadfile2.html"><b>Original Processing.org Example:</b> LoadFile2</a><br>
<script type="application/processing">
Record[] records;
String[] lines;
int recordCount;
PFont body;
int num = 9; // Display this many entries on each screen.
int startingEntry = 0;  // Display from this entry number

void setup() 
{
  size(200, 200);
  fill(255);
  noLoop();
  
  body = loadFont("TheSans-Plain-12.vlw");
  textFont(body);
  
  lines = loadStrings("cars2.tsv");
  records = new Record[lines.length];
  for (int i = 0; i < lines.length; i++) {
    String[] pieces = split(lines[i], '\t'); // Load data into arrayif (pieces.length == 9) {
    records[recordCount] = new Record(pieces);
    recordCount++;
  }
}

void draw() {
  background(0);
  for (int i = 0; i < num; i++) {
    int thisEntry = startingEntry + i;
    text(thisEntry + " > " + records[thisEntry].name, 20, 20 + i*20); // Print name to console
  }
}

void mousePressed() {
  startingEntry += num; 
  if (startingEntry + num > records.length) {
    startingEntry -= num;
  } 
  redraw();
}

class Record {
  String name;
  float mpg;
  int cylinders;
  float displacement;
  float horsepower;
  float weight;
  float acceleration;
  int year;
  float origin;
  public Record(String[] pieces) {
    name = pieces[0];
    mpg = float(pieces[1]);
    cylinders = int(pieces[2]);
    displacement = float(pieces[3]);
    horsepower = float(pieces[4]);
    weight = float(pieces[5]);
    acceleration = float(pieces[6]);
    year = int(pieces[7]);
    origin = float(pieces[8]);
  }
}
</script><canvas width="200" height="200"></canvas></p>
<div style="height:0px;width:0px;overflow:hidden;"><img src='data/cars2.tsv' id='cars2.tsv'/></div>

<pre><b>// All Examples Written by <a href="http://reas.com/">Casey Reas</a> and <a href="http://benfry.com/">Ben Fry</a>
// unless otherwise stated.</b>
Record[] records;
String[] lines;
int recordCount;
PFont body;
int num = 9; // Display this many entries on each screen.
int startingEntry = 0;  // Display from this entry number

void setup() 
{
  size(200, 200);
  fill(255);
  noLoop();
  
  body = loadFont("TheSans-Plain-12.vlw");
  textFont(body);
  
  lines = loadStrings("cars2.tsv");
  records = new Record[lines.length];
  for (int i = 0; i &lt; lines.length; i++) {
    String[] pieces = split(lines[i], '\t'); // Load data into arrayif (pieces.length == 9) {
    records[recordCount] = new Record(pieces);
    recordCount++;
  }
}

void draw() {
  background(0);
  for (int i = 0; i &lt; num; i++) {
    int thisEntry = startingEntry + i;
    text(thisEntry + " &gt; " + records[thisEntry].name, 20, 20 + i*20); // Print name to console
  }
}

void mousePressed() {
  startingEntry += num; 
  if (startingEntry + num &gt; records.length) {
    startingEntry -= num;
  } 
  redraw();
}

class Record {
  String name;
  float mpg;
  int cylinders;
  float displacement;
  float horsepower;
  float weight;
  float acceleration;
  int year;
  float origin;
  public Record(String[] pieces) {
    name = pieces[0];
    mpg = float(pieces[1]);
    cylinders = int(pieces[2]);
    displacement = float(pieces[3]);
    horsepower = float(pieces[4]);
    weight = float(pieces[5]);
    acceleration = float(pieces[6]);
    year = int(pieces[7]);
    origin = float(pieces[8]);
  }
}</pre>
</body>
</html>
